package com.archive.forum.advice;

import com.alibaba.fastjson2.JSON;
import com.archive.forum.dto.ResultDTO;
import com.archive.forum.exception.CustomizeException;
import com.archive.forum.exception.ExceptionEnum;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;

import java.io.IOException;
import java.io.PrintWriter;
//错误处理
@ControllerAdvice
public class CustomizeExceptionHandler {
    //这一个自设的能拦截的错误，其他都是预设拦截
    @ResponseBody
    @ExceptionHandler(CustomizeException.class)
    public Object handleCustomizeException(CustomizeException e, Model model,
                                           HttpServletRequest request,HttpServletResponse response) {
        String contentType = request.getContentType();
        if ("application/json".equals(contentType)) {
            ResultDTO resultDTO;
            //返回JSON
            if(e != null){
                resultDTO =  ResultDTO.errorOf(e);
            }else{
                resultDTO =  ResultDTO.errorOf(ExceptionEnum.SYS_ERROR);
            }
            try {
                response.setContentType("application/json");
                response.setStatus(200);
                response.setCharacterEncoding("UTF-8");
                PrintWriter writer = response.getWriter();
                writer.write(JSON.toJSONString(resultDTO));
                writer.close();
            } catch (IOException ex) {
            }
            return null;
        }else {
            //错误页面跳转
            if(e != null){
                model.addAttribute("error", e.getErrorMsg());
                model.addAttribute("status", e.getErrorCode());
                model.addAttribute("message", "业务异常！");
            }else{
                model.addAttribute("message", ExceptionEnum.SYS_ERROR.getResultMsg());
            }
        }
        return new ModelAndView("error");
    }
    @ExceptionHandler(NullPointerException.class)
    public ModelAndView handleAllException(NullPointerException e, Model model) {
        if(e != null){
            model.addAttribute("error", e.getMessage());
            model.addAttribute("status", HttpStatus.BAD_REQUEST);
            model.addAttribute("message", "空指针异常！");
        }else{
            model.addAttribute("message", "服务出错啦！等会在说吧");
        }
        return new ModelAndView("error");
    }
    @ExceptionHandler(NoHandlerFoundException.class)
    public ModelAndView handleNoHandlerFound(NoHandlerFoundException e,Model model) {
        if(e != null){
            model.addAttribute("error", e.getMessage());
            model.addAttribute("status", HttpStatus.NOT_FOUND);
            model.addAttribute("message", "请求的页面不存在！");
        }else {
            model.addAttribute("message", "服务出错啦！等会在说吧");
        }
        return new ModelAndView("error");
    }
    @ExceptionHandler(Exception.class)
    public ModelAndView handleAllException(Exception e, Model model) {
        if(e != null){
            model.addAttribute("error", e.getMessage());
            model.addAttribute("status", HttpStatus.BAD_REQUEST);
            model.addAttribute("message", "未知异常！");
        }else{
            model.addAttribute("message", "服务出错啦！等会在说吧");
        }
        return new ModelAndView("error");
    }

}
